﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web.Mvc;

using mvcForum.Core;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Specifications;

using mvcForum.Web.Controllers;
using mvcForum.Web.ViewModels;
using mvcForum.Web.Areas.ForumAdmin.ViewModels.List;
using mvcForum.Core.Interfaces.Data;
using mvcForum.Web.Interfaces;

namespace mvcForum.Web.Areas.ForumAdmin.Controllers {

	public class HomeController : ForumAdminBaseController {

		public HomeController(IWebUserProvider userProvider, IContext context/*, IApplicationCache cache*/)
			: base(userProvider, context) {
		}

		public ActionResult Index() {
			ForumSettings setting = this.GetRepository<ForumSettings>().ReadOne(new ForumSettingsSpecifications.SpecificKey("InstallDate"));
			DateTime installDate = DateTime.Parse(setting.Value);

			Statistics stats = new Statistics {
				TopicCount = this.GetRepository<Topic>().ReadMany(new TopicSpecifications.Visible()).Count(),
				PostCount = this.GetRepository<Post>().ReadMany(new PostSpecifications.Visible()).Count(),
				AttachmentCount = this.GetRepository<Attachment>().ReadAll().Count(),
				UserCount = this.GetRepository<ForumUser>().ReadMany(new ForumUserSpecifications.NotDeleted()).Count(),
				AttachmentSize = this.GetRepository<Attachment>().ReadAll().Sum(a => a.Size),
				InstallDate = installDate,
				Days = DateTime.UtcNow.Subtract(installDate).Days
			};
			return View(stats);
		}

		#region Private methods
		[NonAction]
		internal static void BuildPath(Board board, Dictionary<String, String> path, String area) {
			path.Add(String.Format("/{0}/board/edit/{1}", area, board.Id), board.Name);
		}

		[NonAction]
		internal static void BuildPath(Category category, Dictionary<String, String> path, String area) {
			BuildPath(category.Board, path, area);
			path.Add(String.Format("/{0}/category/edit/{1}", area, category.Id), category.Name);
		}

		[NonAction]
		internal static void BuildPath(Core.Forum forum, Dictionary<String, String> path, String area) {
			BuildPath(forum.Category, path, area);
			if (forum.ParentForum != null) {
				BuildForumPath((forum.ParentForum), path, area);
			}
			path.Add(String.Format("/{0}/forum/edit/{1}", area, forum.Id), forum.Name);
		}

		[NonAction]
		internal static void BuildForumPath(Core.Forum forum, Dictionary<String, String> path, String area) {
			if (forum.ParentForum != null) {
				BuildForumPath(forum.ParentForum, path, area);
			}
			path.Add(String.Format("/{0}/forum/edit/{1}", area, forum.Id), forum.Name);
		}

		[NonAction]
		internal static void AddCategories(Board board, BoardViewModel model) {
			List<CategoryViewModel> catModels = new List<CategoryViewModel>();
			foreach (var cat in board.Categories) {
				catModels.Add(new CategoryViewModel(cat));
			}
			model.Categories = new ReadOnlyCollection<CategoryViewModel>(catModels);
		}
		#endregion
	}
}